#include "wgtpara.h"

#include <QGridLayout>
#include <QDebug>

WgtPara::WgtPara(int Type,QString text1,QString text2,QWidget *parent)
    :m_Type(0),QWidget{parent}
{
    m_Type = Type;
    //m_Type = 3;
    m_EngText = text2;
    QGridLayout *gloyout = new QGridLayout(this);

    m_LabelText = new QLabel(this);
    m_LabelText->setText(text1);

    m_CkeckBox = new QCheckBox(this);
    m_CkeckBox->setMinimumWidth(50);
    connect(m_CkeckBox,SIGNAL(clicked(bool)),this,SLOT(slot_checkClick(bool)));

    m_doubleSpinBox = new QDoubleSpinBox(this);
    m_doubleSpinBox->setMaximum(100);
    m_doubleSpinBox->setMinimum(-100);
    connect(m_doubleSpinBox,SIGNAL(textChanged(QString)),this,SLOT(slot_doubleSpilBox(QString)));

    m_ComboBox = new QComboBox(this);
    m_ComboBox->addItems(QStringList()<<"false"<<"true");
    m_ComboBox->setMinimumHeight(20);
    connect(m_ComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(slot_ComBoxText(QString)));


    m_LineEdit = new QLineEdit(this);
    m_LineEdit->setText("NULL");
    connect(m_LineEdit,SIGNAL(returnPressed()),this,SLOT(slot_LineEdit()));

    gloyout->addWidget(m_LabelText,0,0,1,2);
    if(m_Type == 1)
    {
        gloyout->addWidget(m_CkeckBox,1,0);
        gloyout->addWidget(m_doubleSpinBox,1,1);
        m_ComboBox->hide();
        m_LineEdit->hide();
    }
    else if(m_Type == 2)
    {
        gloyout->addWidget(m_CkeckBox,1,0);
        gloyout->addWidget(m_ComboBox,1,1);
        m_doubleSpinBox->hide();
        m_LineEdit->hide();
    }
    else
    {
        gloyout->addWidget(m_CkeckBox,1,0);
        gloyout->addWidget(m_LineEdit,1,1);
        m_doubleSpinBox->hide();
        m_ComboBox->hide();
    }
    this->setLayout(gloyout);


    m_ComboBox->setEnabled(false);
    m_doubleSpinBox->setEnabled(false);
}

void WgtPara::slot_checkClick(bool ischeck)
{
    m_ComboBox->setEnabled(ischeck);
    m_doubleSpinBox->setEnabled(ischeck);
    m_LineEdit->setEnabled(ischeck);
    if(!ischeck)
    {
        emit sig_ParaData(m_EngText,"NULL");
        m_LineEdit->setText("NULL");
    }
    else{
        if(m_LineEdit->text() == "NULL")
            m_LineEdit->setText("0");
        if(m_Type == 1)
        {
            emit sig_ParaData(m_EngText,m_doubleSpinBox->text());
        }
        else if(m_Type == 2)
        {
            emit sig_ParaData(m_EngText,m_ComboBox->currentText());
        }
        else if(m_Type == 3)
        {
            emit sig_ParaData(m_EngText,m_LineEdit->text());
        }
    }
}

void WgtPara::slot_ComBoxText(QString text)
{
    qDebug()<<"slot_ComBoxText="<<continueCmBox;
    if(continueCmBox)
    {
        continueCmBox = false;
        return;
    }
    if(m_CkeckBox->isChecked())
        emit sig_ParaData(m_EngText,text);
}

void WgtPara::slot_doubleSpilBox(QString text)
{
    qDebug()<<"slot_doubleSpilBox="<<continueDSBox;
    if(continueDSBox)
    {
        continueDSBox = false;
        return;
    }
    if(m_CkeckBox->isChecked())
        emit sig_ParaData(m_EngText,text);
}

void WgtPara::slot_LineEdit()
{
    if(m_CkeckBox->isChecked())
        emit sig_ParaData(m_EngText,m_LineEdit->text());
}

void WgtPara::slot_setPart(QString data)
{
    if(data == "NULL")
    {
        m_CkeckBox->setChecked(false);
        m_ComboBox->setEnabled(false);
        m_doubleSpinBox->setEnabled(false);
        m_LineEdit->setEnabled(false);
        if(m_ComboBox->currentIndex() != 0)
        {
            continueCmBox = true;
            m_ComboBox->setCurrentIndex(0);
        }
        if(m_doubleSpinBox->value() != 0)
        {
            continueDSBox = true;
            m_doubleSpinBox->setValue(0);
        }
        m_LineEdit->setText("NULL");
    }
    else
    {
        m_CkeckBox->setChecked(true);
        m_ComboBox->setEnabled(true);
        m_doubleSpinBox->setEnabled(true);
        if(m_Type == 1)
        {
            if(m_doubleSpinBox->value() != data.toDouble())
            {
                continueDSBox = true;
                m_doubleSpinBox->setValue(data.toDouble());
            }
        }
        else if(m_Type == 2)
        {

            if(data == "true")
            {
                if(m_ComboBox->currentIndex() != 1)
                {
                    continueCmBox = true;
                    m_ComboBox->setCurrentIndex(1);
                }
            }
            else
            {
                if(m_ComboBox->currentIndex() != 0)
                {
                    continueCmBox = true;
                    m_ComboBox->setCurrentIndex(0);
                }
            }
        }
        else if(m_Type == 3)
        {
            m_LineEdit->setText(data);
        }
    }
}
